﻿const gulp = require('gulp');
const ts = require('gulp-typescript');
const tsProj = ts.createProject('tsconfig.json');
const webConfig = require("./webconfig").webconfig;
const fs = require("fs");
const path = require("path");

gulp.task('tsc', () => {
    var result = tsProj.src().pipe(tsProj());
    return result.pipe(gulp.dest('./'));
});

gulp.task("copy", ['tsc'], () => {
    var files = [
        'package.json',
        'README.md',
        "webconfig.js"
    ];
    gulp.src(files).pipe(gulp.dest('publish'));

    gulp.src([
        "certificate/ca.crt",
        "certificate/ca.csr",
        "certificate/ca.key"
    ]).pipe(gulp.dest('publish/certificate'));

    gulp.src(["./src/common/**/*.js"]).pipe(gulp.dest("publish/src/common/"));
    gulp.src(["./src/domain/**/*.js"]).pipe(gulp.dest("publish/src/domain/"));
    gulp.src(["./src/middleware/**/*.js"]).pipe(gulp.dest("publish/src/middleware/"));
    gulp.src(["./src/log/**/*.js"]).pipe(gulp.dest("publish/src/log/"));
    gulp.src(["./src/controller/verification/**/*.js"]).pipe(gulp.dest("publish/src/controller/verification"));
    gulp.src([
        "./src/app.js",
        "./src/server.js"
    ]).pipe(gulp.dest("publish/src/"));

    var svr_out = [];

    for (var svr in webConfig.baseService) {
        svr_out.push("./src/" + webConfig.baseService[svr].src + ".js");
    }
    svr_out.push("./src/service/serviceException.js");
    gulp.src(svr_out).pipe(gulp.dest("publish/src/service/"));

    var ctrl_out = [];
    for (var item of webConfig.router) {
        ctrl_out.push("./src/" + item.src + ".js");
    }
    ctrl_out.push("./src/controller/webException.js");
    gulp.src(ctrl_out).pipe(gulp.dest("publish/src/controller/"));

    if (webConfig.dao) {
        gulp.src("./src/dao/**/*.js").pipe(gulp.dest("publish/src/dao/"));
    }

});

gulp.task('publish', ['tsc', 'copy']);
gulp.task('default', ['publish']);